#include"LockGuard.hpp"
#include<iostream>
#include<unistd.h>
#include<vector>
using namespace std;
volatile int tickets = 10;
void Run(ThreadDate* td,string name)
{
    int ct = 0;
    while (1)
    {
        LockGuard ld(td);//RAII风格锁
        if (tickets > 0)
        {
            usleep(1000);
            tickets--;
            ct++;
        }
        else
        {
            break;
        }
        
    }
    cout << name << " get tickets:" << ct << endl;
}
int main()
{
    pthread_mutex_t mutex;
    pthread_mutex_init(&mutex, nullptr);
    vector<Thread> v;
    int n = 3;
    for (int i = 0; i < n; i++)
    {
        string name = "syx" + to_string(i + 1);
        ThreadDate *td = new ThreadDate(name, &mutex);
        v.emplace_back(name, Run, td);
    }
    for(auto &i:v)
    {
        i.Start();
    }
    for(auto &i:v)
    {
        i.Join();
    }
    cout << tickets << endl;
    pthread_mutex_destroy(&mutex);

    return 0;
}